<?php

class Tools_Common
{
    public function json($error, $data = array()){
        if ($error === TRUE) {
            $res = array(
                'code'=> 0,
                'msg' => '成功'
            );
            if(count($data) > 0) $res['data'] = $data;

            return json_encode($res);
        }

        if ($error === FALSE) {
            return json_encode(
                array(
                    'code'=> 1,
                    'msg' => array(array('失败')),
                ));
        }
        if (is_object($error)) {
            return json_encode(
                array(
                    'code'=> 1,
                    'msg' => $error->messages
                ));
        }
        if (is_string($error)) {
            return json_encode(
                array(
                    'code'=> 1,
                    'msg' => $error
                ));
        }
        if (is_array($error)) {
            return json_encode(
                array(
                    'code'=> 1,
                    'msg' => array($error)
                ));
        }
    }
    
    /**
     * 格式化时间
     */
    public function time_format( $time = 0 )
    {
        $res = '';

        $now = strtotime(date('Y-m-d H:i:s'));
        $today = strtotime(date('Y-m-d'));

        if($time >= $today){
            $cz = $now - $time;
            if($cz/60 > 60){
                $ctime = floor($cz/60/60);
                $res = $ctime.'小时前';
            }elseif($cz/60 < 60){
                $ctime = floor($cz/60);
                if($ctime == 0){
                    $res = '刚刚';
                }else{
                    $res = $ctime.'分钟前';
                }
            }
        }else{
            $res = date('n-j',$time);
        }

        return $res;
    }
}